package com.smart.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class PropsUtil {
    /**
     * 加载属性文件
     * @param fileName
     * @return
     */
    public static Properties loadProps(String fileName) {
        Properties props = null;
        InputStream is = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            props = new Properties();
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return props;
    }

    /**
     * 获取字符型属性
     * @param props
     * @param key
     * @return
     */
    public static String getString(Properties props,String key){
        return getString(props,key,"");
    }
    public static String getString(Properties props,String key,String value){
        String val=value;
        if(props.containsKey(key)){
            val=props.getProperty(key);
        }
        return val;
    }

    /**
     * 获取数值型属性
     * @param props
     * @param key
     * @return
     */

    public static int getInt(Properties props,String key){
        return getInt(props,key,0);
    }
    public static int getInt(Properties props,String key,int value){
        int val=value;
        if(props.containsKey(key)){
            val=CastUtil.castInt(props.getProperty(key));
        }
        return val;
    }
    /**
     * 获取布尔型属性
     * @param props
     * @param key
     * @return
     */
    public static boolean getBoolean(Properties props,String key){
        return getBoolean(props,key,false);
    }
    public static boolean getBoolean(Properties props,String key,Boolean value){
        Boolean val=value;
        if(props.containsKey(key)){
            val=CastUtil.castBoolean(props.getProperty(key));
        }
        return val;
    }


}
